#include "mainwindow.h"
#include "ui_mainwindow.h"


#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initMembers();
    /// int ret = _handler.startListen();
    /// 实时数据流传输到UI
    HandleHelper::GetInstance()->_send2Form.connect(boost::bind(&MainWindow::slot_recvHandler, this, _1));
    HandleHelper::GetInstance()->_sendCurrentPos.connect(boost::bind(&MainWindow::updateRobotPosAndAxle, this, _1));

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(slot_updateUI()));
    timer->start(100);

    initUI();

    connect(lineChart,&LineChart::updateChart,this,&MainWindow::slot_changeChart);
    connect(horBarChart,&HorBarChart::updateChart,this,&MainWindow::slot_changeChart);
}

MainWindow::~MainWindow()
{
    HandleHelper::GetInstance()->StopService();
    delete ui;
}

void MainWindow::slot_updateUI()
{
    ///
    ui->tb_1->setEnabled(d_handler.enMotion);
    ui->tb_2->setEnabled(d_handler.outSilk);
    ui->tb_3->setEnabled(d_handler.inSilk);
    ui->tb_4->setEnabled(d_handler.isPause);


//    ui->lb_o->setText(QString::number(d_handler.a));
//    ui->lb_a->setText(QString::number(d_handler.b));
//    ui->lb_t->setText(QString::number(d_handler.c));
//    ui->lb_x->setText(QString::number(d_handler.x));
//    ui->lb_y->setText(QString::number(d_handler.y));
//    ui->lb_z->setText(QString::number(d_handler.z));
    ui->lb_speed->setText(QString::number(d_handler.speed));
    QString lab ;
    lab = d_handler.coordinate ? "Base" : "Tool" ;
    ui->lb_coord->setText(lab);
    lab = d_handler.motionType ? "XYZ":"OAT";
    ui->lb_motion->setText(lab);
    lab = d_handler.criticalStop ? "急停按下":"急停松开";
    ui->lb_crit->setText(lab);
}


//更新界面机器人位置
void MainWindow::updateRobotPosAndAxle(RobotPos pos)
{
    ui->lb_x->setText(QString::number(pos.x));
    ui->lb_y->setText(QString::number(pos.y));
    ui->lb_z->setText(QString::number(pos.z));
    ui->lb_o->setText(QString::number(pos.a));
    ui->lb_a->setText(QString::number(pos.b));
    ui->lb_t->setText(QString::number(pos.c));
//    auto axle = _robot->getCurrAxle() ;
//    ui->currA1->setText(QString::number(axle.a1));
//    ui->currA2->setText(QString::number(axle.a2));
//    ui->currA3->setText(QString::number(axle.a3));
//    ui->currA4->setText(QString::number(axle.a4));
//    ui->currA5->setText(QString::number(axle.a5));
//    ui->currA6->setText(QString::number(axle.a6));
}



void MainWindow::slot_changeChart(int index)
{
    if(index == 0){
        ui->st_w->setCurrentIndex(2);
    }else{
        ui->st_w->setCurrentIndex(1);
    }
}

void MainWindow::slot_recvHandler(ST_HD_T *data)
{
    d_handler = *data;
}

void MainWindow::initUI()
{
    //
    lineChart = new  LineChart;
    horBarChart = new HorBarChart;

    controlCenter = new DebugForm;
    _stack = new QStackedWidget;

    ui->st_w->addWidget(horBarChart);
    ui->st_w->addWidget(lineChart);

    ui->st_w->setCurrentIndex(1);

//    _stack->addWidget(lineChart);
    //    _stack->addWidget(horBarChart);
}

void MainWindow::initMembers()
{
    d_handler.a = 0;
    d_handler.b = 0;
    d_handler.c = 0;
    d_handler.x = 0;
    d_handler.y = 0;
    d_handler.z = 0;
    d_handler.coordinate = false;
    d_handler.criticalStop = false;
    d_handler.enMotion = false;
    d_handler.inSilk = false;
    d_handler.outSilk = false;
    d_handler.motionType = false;
    d_handler.speed = 0;
    d_handler.isPause = false;

}


void MainWindow::on_start_clicked()
{
    controlCenter->show();
    //HandleHelper::GetInstance()->ConnectHandlerExec();
}
